/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class StructureNotation extends ItemDefinition {

	private SymbolId symbolId = null;
	
	private Loci loci = null;
	private Format format = null;
	private Visible visible = null;
	private Constructor constructor = null;
	private Synonym synonym = null;
	
	private StructureNotation(){
		super(null);
	}

    public StructureNotation(SymbolId symbolId, Loci loci, Format format, Visible visible, Constructor constructor, Synonym synonym) throws DliException {
    	super(null);
		this.symbolId = symbolId;
		this.loci = loci;
		this.format = format;
		this.visible = visible;
		this.constructor = constructor;
		this.synonym = synonym;
    }
	
	public static ItemDefinition newInstance(String string) throws DliParseException {
		StructureNotation structureNotation = new StructureNotation();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in StructureNotation");
		}
		if (!string.substring(0, position).equals("structnot")) {
			throw new DliParseException("["+string+"] is not valid in StructureNotation");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in StructureNotation");
		}
		structureNotation.symbolId = SymbolId.newInstance(string.substring(0, positionComma));
		string = string.substring(positionComma+1, string.length());
		positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in StructureNotation");
		}
		String type = null;
		while (positionComma != -1) {
			position = string.indexOf("(");
			int positionCommaTest = string.indexOf(",");
			if (positionCommaTest != 0 && positionComma<position){
				position = positionCommaTest;
			}
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in StructureNotation");
			}
			type = string.substring(0, position);
			if (type.equals("$loci")){
				structureNotation.loci = Loci.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$format")){
				structureNotation.format = Format.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$visible")){
				structureNotation.visible = Visible.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$constructor")){
				structureNotation.constructor = Constructor.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$synonym")){
				structureNotation.synonym = Synonym.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in StructureNotation");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		if (string.equals("$synonym")){
			structureNotation.synonym = Synonym.newInstance(string);
		} else {
			position = string.indexOf("(");
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in StructureNotation");
			}
			type = string.substring(0, position);
			if (type.equals("$loci")){
				structureNotation.loci = Loci.newInstance(string);
			} else if (type.equals("$format")){
				structureNotation.format = Format.newInstance(string);
			} else if (type.equals("$visible")){
				structureNotation.visible = Visible.newInstance(string);
			} else if (type.equals("$constructor")){
				structureNotation.constructor = Constructor.newInstance(string);
			} else if (type.equals("$synonym")){
				structureNotation.synonym = Synonym.newInstance(string);
			} else {
				throw new DliParseException("["+string+"] is not valid in StructureNotation");
			}
		}
		return structureNotation;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("structnot(");
		if (symbolId != null){
			sb.append(symbolId.toString());
		}
		if (loci != null){
			sb.append(",");
			sb.append(loci.toString());
		}
		if (format != null){
			sb.append(",");
			sb.append(format.toString());
		}
		if (visible != null){
			sb.append(",");
			sb.append(visible.toString());
		}
		if (constructor != null){
			sb.append(",");
			sb.append(constructor.toString());
		}
		if (synonym != null){
			sb.append(",");
			sb.append(synonym.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}

	public SymbolId getSymbolId() {
		return symbolId;
	}

	public Loci getLoci() {
		return loci;
	}

	public Format getFormat() {
		return format;
	}

	public Visible getVisible() {
		return visible;
	}

	public Constructor getConstructor() {
		return constructor;
	}

	public Synonym getSynonym() {
		return synonym;
	}
	
}
